﻿#region File Description
/****************************************************************************
*                                                                           *
* Game Manager.                                                             *
*                                                                           *
* Copyright (c) Ionescu Marius. All rights reserved(2013).                  *
*                                                                           *
****************************************************************************/
#endregion
namespace Silverlight3D
{
    using System;
using System.Collections.Generic;
using System.Windows;

    /// <summary>
    /// Helper methods operating on Vectors.
    /// </summary>
    public static class VectorHelper
    {
        /// <summary>
        /// Gets the 'dot product' of two Vectors.  This is a measure of how parallel the two Vectors are.
        /// 1 implies the Vectors are parallel.  0 implies the Vectors are perpendicular.  -1 implies the Vectors are
        /// parallel, but pointing in opposite directions.
        /// </summary>
        /// <returns>The dot product of the two Vectors.</returns>
        public static double DotProduct(Vector v1, Vector v2)
        {
            v1 = Normalise(v1);
            v2 = Normalise(v2);

            return v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z;
        }

        /// <summary>
        /// Gets the 'cross product' of two Vectors, which is a Vector perpendicular to the two input Vectors.
        /// </summary>
        /// <returns>A Vector perpendicular to the two input Vectors.</returns>
        public static Vector CrossProduct(Vector v1, Vector v2)
        {
            Vector result = new Vector();

            result.X = v1.Y * v2.Z - v1.Z * v2.Y;
            result.Y = v1.Z * v2.X - v1.X * v2.Z;
            result.Z = v1.X * v2.Y - v1.Y * v2.X;

            return result;
        }

        /// <summary>
        /// Adjusts the Vector so as to make its Magnitude equal 1, while maintaining its direction.
        /// </summary>
        /// <returns>The normalised Vector.</returns>
        public static Vector Normalise(Vector vector)
        {
            double magnitude = vector.Magnitude();

            vector.X = vector.X / magnitude;
            vector.Y = vector.Y / magnitude;
            vector.Z = vector.Z / magnitude;

            return vector;
        }

        /// <summary>
        /// Gets the square of the distance between two Vectors (Pythagoras).
        /// </summary>
        /// <returns>The square of the distance between the two Vectors.</returns>
        public static double DistanceSquared(Vector v1, Vector v2)
        {
            double dx = v1.X - v2.X;
            double dy = v1.Y - v2.Y;
            double dz = v1.Z - v2.Z;

            return dx * dx + dy * dy + dz * dz;
        }

        /// <summary>
        /// Gets the distance between two Vectors (Pythagoras).
        /// </summary>
        /// <returns>The distance between the two Vectors.</returns>
        public static double Distance(Vector v1, Vector v2)
        {
            double dx = v1.X - v2.X;
            double dy = v1.Y - v2.Y;
            double dz = v1.Z - v2.Z;

            return Math.Sqrt(dx * dx + dy * dy + dz * dz);
        }
    }
}